﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NPSH_Check                             // Changed name from NPSH_Check to NPSHCalc
{
    public class Calc
    {
        public double SG = 1.3;           // user input e.g. 1.3
        public double e = 0.05 / 1000;
        public double k = 0.05;
        public double n = 1;
        public double d = 15;
        public double pfc = 0; // pipe fitting choice

        private Outputs _outputs = new Outputs();

        public Outputs Start()
        {
            _outputs.Hatm = (101325 * Math.Pow(1 - 2.25577 * Math.Pow(10, -5) * UserInputs.Ele, 5.25588)) * 102 / 1000000;                //101325*POWER(1-2.25577*POWER(10,-5)*I17,5.25588)
            _outputs.Hv = 10.33 * Math.Pow(10, 8.07131 - 1730.63 / (233.426 + UserInputs.Temps)) / 760;

            _outputs.Vsi = GetVsi(UserInputs.Q, UserInputs.Dsi);
            _outputs.Hvs = Math.Pow(_outputs.Vsi, 2) / (2 * 9.81);
            _outputs.Hf = UserInputs.f * UserInputs.Ls * (Math.Pow(_outputs.Vsi, 2)) * 1000 / (UserInputs.Dsi * 2 * 9.81);
            _outputs.ρm = UserInputs.SG * 1000;
            _outputs.Rn = _outputs.ρm * UserInputs.Dnominal * _outputs.Vsi / (UserInputs.µ * 1000);

            switch (UserInputs.RoughnessCoefficientChoice)
            {
                case ERoughnessCoefficientType.GlassBrSteelCuPb:
                    e = 0.015;
                    break;
                case ERoughnessCoefficientType.FeSteel:
                    e = 0.052;
                    break;
                case ERoughnessCoefficientType.GI:       // 0.15 to 0.6
                    e = 0.3;
                    break;
                case ERoughnessCoefficientType.Concrt:            // 0.3 to 3
                    e = 2.0;
                    break;
                case ERoughnessCoefficientType.CIU:    // 0.25 to 0.6
                    e = 0.015;
                    break;
                case ERoughnessCoefficientType.CIC:          // 0.15 to 0.3
                    e = 0.015;
                    break;
                case ERoughnessCoefficientType.PPA:        // 0.0015 to 0.003
                    e = 0.015;
                    break;
                case ERoughnessCoefficientType.VC:               // 0.15 to 0.6
                    e = 0.6;
                    break;
            }

            switch (UserInputs.KinletChoice)
            {
                case EKinletType.InwardProjecting:
                    k = 0.078;
                    break;
                case EKinletType.SharpEdgedrOverd0:
                    k = 0.05;
                    break;
                case EKinletType.rOverD02:
                    k = 0.28;
                    break;
                case EKinletType.rOverD04:
                    k = 0.24;
                    break;
                case EKinletType.rOverD10:
                    k = 0.015;
                    break;
                case EKinletType.rOverD15:
                    k = 0.09;
                    break;
                case EKinletType.PSeR:
                    k = 0.04;
                    break;
            }

            switch (UserInputs.NominalDiameterChoice)
            {
                case ENominalDiameterType.Fifteen:
                    d = 1;
                    break;
                case ENominalDiameterType.Twenty:
                    d = 2;
                    break;
                case ENominalDiameterType.TwentyFive:
                    d = 3;
                    break;
                case ENominalDiameterType.ThrityTwo:
                    d = 4;
                    break;
                case ENominalDiameterType.Fourty:
                    d = 5;
                    break;
                case ENominalDiameterType.Fifty:
                    d = 6;
                    break;
                case ENominalDiameterType.SixtyFive:
                    d = 7;
                    break;
                case ENominalDiameterType.OneHundred:
                    d = 8;
                    break;
                case ENominalDiameterType.OneTwentyFive:
                    d = 9;
                    break;
                case ENominalDiameterType.OneHundredFifty:
                    d = 10;
                    break;
                case ENominalDiameterType.TwoHundred:
                    d = 11;
                    break;
                case ENominalDiameterType.ThreeHundred:
                    d = 12;
                    break;
                case ENominalDiameterType.FourFifty:
                    d = 13;
                    break;
            }

            switch (UserInputs.PipeFittingChoice)
            {
                case EPipeFittingType.GTV:
                    pfc = 1;
                    break;
                case EPipeFittingType.GBV:
                    pfc = 2;
                    break;
                case EPipeFittingType.AGV:
                    pfc = 3;
                    break;
                case EPipeFittingType.AGVa:
                    pfc = 4;
                    break;
                case EPipeFittingType.BLV:
                    pfc = 5;
                    break;
                case EPipeFittingType.BFV:
                    pfc = 6;
                    break;
                case EPipeFittingType.PVS:
                    pfc = 7;
                    break;
                case EPipeFittingType.PV3TF:
                    pfc = 8;
                    break;
                case EPipeFittingType.PVBF:
                    pfc = 9;
                    break;
                case EPipeFittingType.STDE90:
                    pfc = 10;
                    break;
                case EPipeFittingType.STDE45:
                    pfc = 11;
                    break;
                case EPipeFittingType.SCV1:
                    pfc = 12;
                    break;
                case EPipeFittingType.SCV2:
                    pfc = 13;
                    break;
                case EPipeFittingType.LCV1:
                    pfc = 14;
                    break;
                case EPipeFittingType.LCV2:
                    pfc = 15;
                    break;
                case EPipeFittingType.TDCV1:
                    pfc = 16;
                    break;
                case EPipeFittingType.TDCV2:
                    pfc = 17;
                    break;
                case EPipeFittingType.FVWSPD:
                    pfc = 18;
                    break;
                case EPipeFittingType.STV_FVSHD1:
                    pfc = 19;
                    break;
                case EPipeFittingType.STV_FVSHD2:
                    pfc = 20;
                    break;
                case EPipeFittingType.STV_FVSHD3:
                    pfc = 21;
                    break;
                case EPipeFittingType.STV_FVSHD4:
                    pfc = 22;
                    break;
                case EPipeFittingType.STV_FVSHD5:
                    pfc = 23;
                    break;
                case EPipeFittingType.STV_FVSHD6:
                    pfc = 24;
                    break;
            }

            _outputs.ff = 8.0 * Math.Pow((Math.Pow((8.0 / _outputs.Rn), 12.0) + (Math.Pow(Math.Pow((37530.0 / _outputs.Rn), 16.0) + Math.Pow(-2.457 * Math.Log(Math.Pow((7.0 / _outputs.Rn), 0.9) + (0.27 * e / UserInputs.Dsi)), 16.0), -1.5))), 1.0 / 12.0);

            _outputs.NPSHa = _outputs.Hatm + UserInputs.Hes - _outputs.Hf - _outputs.Hv;
            _outputs.Hinlet = k * _outputs.Hvs;

            if (_outputs.Rn > 2200)
            {
                _outputs.Message1 = "Turbulant Flow";
            }
            else
            {
                _outputs.Message1 = "Laminar Flow";
            }

            if (_outputs.NPSHa > UserInputs.NPSHr)
            {
                _outputs.Message = "Pump will not cavitate";
            }
            else
            {
                _outputs.Message = "Pump will cavitate";
            }

            return _outputs;
        }

        public double GetVsi(double Q, double Dsi)
        {
            return 4 * Q * 1000 / (Math.PI * Math.Pow(Dsi, 2));
        }
    }
}